import javax.swing.plaf.basic.BasicComboBoxUI.ItemHandler;

import org.powerbot.core.script.job.Task;
import org.powerbot.core.script.job.state.Node;
import org.powerbot.game.api.methods.Widgets;
import org.powerbot.game.api.methods.input.Mouse;
import org.powerbot.game.api.methods.interactive.Players;
import org.powerbot.game.api.methods.node.GroundItems;
import org.powerbot.game.api.methods.node.SceneEntities;
import org.powerbot.game.api.methods.tab.Inventory;
import org.powerbot.game.api.util.Random;
import org.powerbot.game.api.wrappers.interactive.Player;
import org.powerbot.game.api.wrappers.node.SceneObject;


public class CutTree extends Node{

	@Override
	public boolean activate() {
		// TODO Auto-generated method stub
		return Inventory.getCount() >= 27 && SceneEntities.getNearest(IDS.trees) != null || Inventory.getCount(1511)>0 && Inventory.getCount() > 20;
	}

	@Override
	public void execute() {
		Fisher.dropping = false;
		Fisher.banking = false;
		Player p = Players.getLocal();
		//if(p.getAnimation() == -1)
		SceneObject fire = SceneEntities.getNearest(70755);
		if(!waitForAnimation(2000))
		{

			if(Widgets.get(1179).getChild(16).visible())
			{
				Widgets.get(1179).getChild(16).click(true);
			}
			if(Widgets.get(1370).getChild(20).visible())
			{
				Widgets.get(1370).getChild(20).click(true);
			}
			
			if(fire!=null)
			{
				if(!waitForAnimation(3000))
				{
					fire.interact("Use");
				}
				//Widgets.get(1179).getChild(16).click(true);
				
			}
			else if(Inventory.getCount(1511)>0)
				Inventory.getItem(1511).getWidgetChild().interact("Light");
			else if(p.getAnimation() == -1)
			{
				if(GroundItems.getNearest(1511)!=null)
					GroundItems.getNearest(1511).interact("Light");
				else
				{
					if(Inventory.isFull())
					{
						Inventory.getItem(IDS.fish).getWidgetChild().interact("drop");
						Fisher.cooking = true;
					}						
					else 
						SceneEntities.getNearest(IDS.trees).click(true);
				}
			}
			if(Inventory.getCount(IDS.fish)==0)
			{
				//cooking done
				Fisher.cooking = false;
			}
			Task.sleep(Random.nextInt(100, 300));
		}
		// TODO Auto-generated method stub
		//logs in invo? light them,
		//cut tree down for logs if no fire on ground
		//fire on ground? skip log cutting and cook.
		//
		
		
	}
	public boolean waitForAnimation(int timeout) {
		long time = System.currentTimeMillis();
		while (System.currentTimeMillis() - time < timeout) {
			if (Players.getLocal().getAnimation() > -1) {
				return true;
			}
			sleep(5, 15);
		}
		return false;
	}
}
